﻿using System;
using System.Globalization;
using System.Reflection;
using System.Resources;

namespace NetBay.Core.DataAccess
{

    [AttributeUsageAttribute(AttributeTargets.Field, AllowMultiple = false)]
    public class ErrorMessageAttribute : Attribute
    {

        private readonly Type _errorMessageResourceType;
        private readonly string _errorMessageResourceName;
        private ResourceManager _manager;

        /// <summary>
        /// Gets the error message.
        /// </summary>
        public string ErrorMessage
        {
            get { return GetResourceString(); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorMessageAttribute"/> class.
        /// </summary>
        /// <param name="ResourceType">Type of the resource.</param>
        /// <param name="ResourceName">Name of the resource.</param>
        public ErrorMessageAttribute(Type ResourceType, string ResourceName)
        {
            _errorMessageResourceType = ResourceType;
            _errorMessageResourceName = ResourceName;
        }

        /// <summary>
        /// Loads the resource.
        /// </summary>
        private void LoadResourceManager()
        {
            if (_manager == null)
            {
                _manager = new ResourceManager(_errorMessageResourceType);
            }
        }

        /// <summary>
        /// Gets the resource.
        /// </summary>
        /// <returns></returns>
        private string GetResourceString()
        {
            LoadResourceManager();
            if (_manager != null)
                return _manager.GetString(_errorMessageResourceName);
            return string.Empty;
        }


    }
}
